function [logPDFNoConstant,logPDF]=invWishartPDF(W,v,S)
% 
%  invWishartPDF
% 
% [logPDFNoConstant,logPDF]=invWishartPDF(W,v,S)
% Computes the Log pdf of the Inverse Wishart.
% Notation follows from Lubrano et. al page 305.
% The unormalized pdf will not be computed --hence no need to account for constant--
% if only one output argument is requested
%
%% Input 
% W: [q,q] positive definite matrix 
% S: [q,q] Prior 
% v: [scalar] degrees of freedom
% 
%% Output 
% logPDFNoConstant: log PDF without the normalizing constant 
% logPDF          : log PDF with normalizing constant 
% 
% Replaces old function INVWISH.pdf which is more inefficient 
% 
% If sure that the matrices are symmetric can replace the call of 
% log(det(A)) = mydet(A) which uses the log of the SVD 
%
% Alejandro Justiniano August 30 2013 (C) 
%==========================================================================
q=size(W,1); 
%% Recall W\S = inv(W)*S 
logPDFNoConstant=-0.5*trace(W\S)-0.5*(q+v+1)*log(det(W)); 
if nargout < 2 
    return 
end 
logConstant=zeros(4,1); 
logConstant(1)=v*q*log(2)/2; 
logConstant(2)=q*(q-1)*log(pi)/4; 
logConstant(3)=sum(gammaln(((v+1 -(1:q)))/2)); 
logConstant(4)=(-v/2)*log(det(S));
logConstant=sum(logConstant);

logPDF=logPDFNoConstant-logConstant; 